package com.richapp.launcher.database.system.dao;

import java.util.ArrayList;
import java.util.List;

import com.j256.ormlite.stmt.DeleteBuilder;
import com.richapp.launcher.database.system.SystemBaseDao;
import com.richapp.launcher.database.system.mode.SceneModel;

import android.content.Context;
import android.util.Log;

public class SceneModelDao extends SystemBaseDao<SceneModel>{

	private Context context;

	public SceneModelDao(Context context) {
		super(context,SceneModel.class);
		this.context = context;
	}



	public SceneModel get(String sceneName){
        SceneModel sceneModel = null;
        try {
            sceneModel = gettOp().queryBuilder().where().eq("sceneName", sceneName).queryForFirst();
            if(sceneModel != null){
                sceneModel.setTimeModelList(new ConditionTimeModelDao(context).getList(sceneModel.getSceneName()));
                sceneModel.setSensorModelList(new ConditionSensorModelDao(context).getList(sceneModel.getSceneName()));
                sceneModel.setStatusModelList(new ConditionStatusModelDao(context).getList(sceneModel.getSceneName()));
                sceneModel.setActionModelList(new ActionModelDao(context).getList(sceneModel.getSceneName()));
				sceneModel.setActionInfraredModelList(new ActionInfraredModelDao(context).getList(sceneModel.getSceneName()));
            }
        } catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
        }
        return sceneModel;
    }

    public SceneModel getSceneModelOnly(String sceneName){
        SceneModel sceneModel = null;
        try {
            sceneModel = gettOp().queryBuilder().where().eq("sceneName", sceneName).queryForFirst();
        } catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
        }
        return sceneModel;
    }

	public SceneModel getSceneModelByNameAndUserName(String sceneName,String userName){
		SceneModel sceneModel = null;
		try {
			sceneModel = gettOp().queryBuilder().where().eq("sceneName", sceneName).and().eq("userName",userName).queryForFirst();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return sceneModel;
	}
	
	public List<SceneModel> getList(){
		List<SceneModel> list = new ArrayList<SceneModel>();
		try {
			list = gettOp().queryBuilder().query();
			if(list != null){
				for(SceneModel sm : list){
					sm.setTimeModelList(new ConditionTimeModelDao(context).getList(sm.getSceneName()));
					sm.setSensorModelList(new ConditionSensorModelDao(context).getList(sm.getSceneName()));
					sm.setStatusModelList(new ConditionStatusModelDao(context).getList(sm.getSceneName()));
					sm.setActionModelList(new ActionModelDao(context).getList(sm.getSceneName()));
					sm.setActionInfraredModelList(new ActionInfraredModelDao(context).getList(sm.getSceneName()));
				}
			}
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return list;
	}
	
	public void del(String sceneName){
		try {
			DeleteBuilder<SceneModel, Integer>  delBuilder = gettOp().deleteBuilder();
			delBuilder.where().eq("sceneName", sceneName);
			delBuilder.delete();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
	}
	
	public void delSceneAllData(String sceneName){
		try {
			ConditionTimeModelDao timeDao = new ConditionTimeModelDao(context);
			ConditionStatusModelDao statusDao = new ConditionStatusModelDao(context);
			ConditionSensorModelDao sensorDao = new ConditionSensorModelDao(context);
			ActionModelDao actionDao = new ActionModelDao(context);
			ActionInfraredModelDao actionInfraredDao = new ActionInfraredModelDao(context);
			
			del(sceneName);
			timeDao.del(sceneName);
			statusDao.del(sceneName);
			sensorDao.del(sceneName);
			actionDao.del(sceneName);
			actionInfraredDao.del(sceneName);
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
	}


	public SceneModel getSceneModelByDeviceNumberAndSceneKey(String deviceNumber,String sceneBindKey){
		SceneModel sceneModel = null;
		try {
			sceneModel = gettOp().queryBuilder().where().eq("deviceNumber", deviceNumber).and().eq("sceneBindKey",sceneBindKey).queryForFirst();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return sceneModel;
	}

	//根据绑定的deviceNumber 和 sceneBindKey 删除情景
	public void delByBindKeyAndDeviceNumber(String deviceNumber,String sceneBindKey){
		try {
			DeleteBuilder<SceneModel, Integer>  delBuilder = gettOp().deleteBuilder();
			delBuilder.where().eq("deviceNumber", deviceNumber).and().eq("sceneBindKey",sceneBindKey);
			delBuilder.delete();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
	}


}
